package sort.swap;

import java.util.Arrays;

public class BubbleSort {

	//记录量，排序执行次数
	public int total;
	//判断量，判断排序情况
	public boolean isOK;

	public int[] bubbleSortRe(int[] array) {
		//交换,临时变量
		int temp = 0;
		total = 0;
		for (int i = array.length-1; i > 0; i--) {
			for (int j = array.length-1; j > array.length - i - 1; j--) {
				if(array[j] < array[j-1]) {
					temp = array[j];
					array[j] = array[j-1];
					array[j-1] = temp;
				}
			}
			total++;
		}
		return array;
	}

	public void bubbleSort(int[] array) {
		int temp = 0;
		for (int i = 0; i < array.length - 1; i++) {
			for (int j = 0; j < array.length - i - 1; j++) {
				if (array[j] > array[j+1]) {
					temp = array[j];
					array[j] = array[j+1];
					array[j+1] = temp;
				}
			}
		}
	}

	public int[] bubbleSort1(int[] array) {
		//交换,临时变量
		int temp = 0;
		total = 0;
		for (int i = 0; i < array.length - 1; i++) {
			for (int j = 0; j < array.length - i - 1; j++) {
				if(array[j] > array[j+1]) {
					temp = array[j];
					array[j] = array[j+1];
					array[j+1] = temp;
				}
			}
			total++;
		}
		return array;
	}

	public int[] bubbleSort2(int[] array) {
		//交换,临时变量
		int temp = 0;
		total = 0;

		//外层循环，意味着排序次数
		for (int i = 0; i < array.length - 1; i++) {
			isOK = true;
			//内层循环，意味着比较次数
			for (int j = 0; j < array.length - i - 1; j++) {
				//判断，是否需要置换
				if(array[j] > array[j+1]) {
					temp = array[j];
					array[j] = array[j+1];
					array[j+1] = temp;

					//置换发生，排序未完成
					isOK = false;
				}
			}
			//排序次数自增
			total++;
			//判断，排序情况
			if (isOK) {
				break;
			}
		}
		return array;
	}

	public int[] bubbleSort3(int[] array) {
		//交换,临时变量
		int temp = 0;
		total = 0;

		if (array == null || array.length < 1) {
			return null;
		}
		//外层循环，意味着排序次数
		for (int i = 0; i < array.length - 1; i++) {
			isOK = true;
			//内层循环，意味着比较次数
			for (int j = 0; j < array.length - i - 1; j++) {
				//判断，是否需要置换
				if(array[j] > array[j+1]) {
					temp = array[j];
					array[j] = array[j+1];
					array[j+1] = temp;

					//置换发生，排序未完成
					isOK = false;
				}
			}
			//排序次数自增
			total++;
			//判断，排序情况
			if (isOK) {
				break;
			}
		}
		return array;
	}

	public void printlnSelf(int[] array) {
		if (array == null || array.length < 1) {
			System.out.println("数组错误！");
			return;
		}
		for (int i = 0; i < array.length; i++) {
			if(i == 0) {
				System.out.print("[");
			}
			if (i == array.length-1) {
				System.out.println(array[i]+"]");
			} else {
				System.out.print(array[i]+", ");
			}
		}
	}

	public static void main(String[] args) {
		//对象初始化
		BubbleSort test = new BubbleSort();
		int[] array1_1 = {1,5,4,8,3,30,11};
		int[] array1_2 = {1,2,3,6,9,30,15};
		int[] array2_1 = {1,5,4,8,3,30,11};
		int[] array2_2 = {1,2,3,6,9,30,15};
		int[] array;
		System.out.println("原数组array1：" + Arrays.toString(array1_1));
		System.out.println("原数组array2：" + Arrays.toString(array1_2));

		array = test.bubbleSort1(array1_1);
		System.out.println("Sort1-1：" + Arrays.toString(array));
		System.out.println("执行次数：" + test.total);
		array = test.bubbleSort1(array1_2);
		System.out.println("Sort1-2：" + Arrays.toString(array));
		System.out.println("执行次数：" + test.total);

		array = test.bubbleSort2(array2_1);
		System.out.println("Sort2-1：" + Arrays.toString(array));
		System.out.println("执行次数：" + test.total);
		array = test.bubbleSort2(array2_2);
		System.out.println("Sort2-2：" + Arrays.toString(array));
		System.out.println("执行次数：" + test.total);
	}

	/**
	 public static void main(String[] args) {
	 int array[] = {1,2,6,9,10,56,34,5,98};
	 int array2[] = {1,2,6,9,10,56,34,5,98};
	 int array3[] = {};
	 int arrayre[] = {1,2,6,9,10,56,34,5,98};

	 BubbleSort test = new BubbleSort();
	 System.out.println("原数组：" + Arrays.toString(array));

	 array = test.bubbleSort1(array);
	 System.out.println("Sort1：" + Arrays.toString(array));
	 System.out.println("执行次数：" + test.total);

	 array2 = test.bubbleSort2(array2);
	 System.out.println("Sort2：" + Arrays.toString(array2));
	 System.out.println("执行次数：" + test.total);

	 arrayre = test.bubbleSortRe(arrayre);
	 System.out.println("SortRe：" + Arrays.toString(array2));
	 System.out.println("执行次数：" + test.total);

	 for (int i : array) {
	 System.out.print(i + " ");
	 }
	 System.out.println();
	 test.printlnSelf(array);

	 array3 = test.bubbleSort3(array3);
	 test.printlnSelf(array3);
	 System.out.println("执行次数：" + test.total);
	 }
	 **/

}
